<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <meta http-equiv="content-language" content="zh-CN" />
    

    
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    

    
    <title>用c语言设置定时关机</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

    <link rel="stylesheet" href="/css/stylesheet.css">
    <link rel="stylesheet" href="/css/home.css">

    
    
        <style type="text/css">
        body { background-color: #fbf6ec;}
        </style>
    
    
                
        
        
            <link rel="stylesheet" href="/css/main.css"/>
        




        
        
        
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/styles/github.min.css"  />
         
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/highlight.min.js"></script>
        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/r.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/yaml.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/latex.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/matlab.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/mathematica.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia-repl.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/powershell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/bash.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/shell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/python.min.js"></script>
        
        <script>hljs.initHighlightingOnLoad();</script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
     
     
</head>


<body>
    <script>
        window.addEventListener("resize", resizeThrottler, false);

        var resizeTimeout;
        function resizeThrottler() {
        
        if ( !resizeTimeout ) {
            resizeTimeout = setTimeout(function() {
            resizeTimeout = null;
            actualResizeHandler();
        
            
            }, 66);
        }
        }
        actualResizeHandler()
        function actualResizeHandler() {
                if (/mobile/i.test(navigator.userAgent) || /android/i.test(navigator.userAgent))
                {
                    document.body.classList.add('mobile');
                }else{
                    document.body.classList.remove('mobile');  
                }
    }</script>

    
      
      
            <nav class="navbar navbar-default navbar-static-top" style="opacity: .9" role="navigation">
        <div class="container-fluid">
            
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">

                    <span class="sr-only">Toggle navigation</span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>

                </button>
                <a class="navbar-brand" href="/">zsc</a>
            </div>

            <div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1" style="height: auto;">
                <ul class="nav navbar-nav navbar-right" style="font-size: 100%">
                    
                        
                            
                            <li class=""><a href="/about/">About</a></li>
                            
                            <li class=""><a href="/categories/">Categories</a></li>
                            
                            <li class=""><a href="/">Home</a></li>
                            
                            <li class=""><a href="/tags/">Tags</a></li>
                            
                            <li class=""><a href="/issue/">存在的问题</a></li>
                            
                        
                    
                </ul>
            </div>
        </div>
    </nav>







<div class = "div-content" id='div-content-my' style='display: none;' >
    

    <div class = 'inner-left' id= 'divTableOfContents' style="position:fixed;z-index:999;height: 55%;overflow: scroll;bottom: 5%;width: 22%;top: 25%" >
            <p class="slide slidemy" align = "center">
                <a href="javascript:hidediv();" id="strHref" class="btn-slide">目录收起-</a>
            </p>
            
            <div id="divtocTableOfContents">
            <nav id="TableOfContents">
  <ul>
    <li><a href="#创建定时任务">创建定时任务</a></li>
  </ul>

  <ul>
    <li><a href="#直接注入关机命令">直接注入关机命令</a></li>
    <li><a href="#结合定时任务关机">结合定时任务关机</a></li>
  </ul>
</nav>
            </div>
    </div>
</div>
<script>  
    $(document).ready(function () {
    var demo = $("#divtocTableOfContents").find("a").length;
    if(demo > 2){
        $("div#div-content-my").fadeIn("slow");
    }
        
        
        
        
        
        
        
        
    }); 
</script>  









<div class="inner">
    



    <div class="blog-post">
        
                <div>
            <h2 align="center" id = "singe-h2">
                用c语言设置定时关机
                <time>
                    <br>
                    <span> 
                        <i class="fa fa-user-edit" style="color:#888;font-size: 80%;"></i>
                        zsc 
                    </span>
                    &nbsp 
                    <span>                 
                        <i class="fa fa-calendar-alt" style="color:#888;font-size: 80%;"></i>
                        2018-11-20 
                    </span>
                </time>
                
                
                <div>
                    <ul class="tags">
                        
                        <span>标签:</span>
                        <li><a class="link" href="/tags/r"> #r </a></li>
                        
                        <span> </span>
                        
                    </ul>
                    
                </div>
            </h2>
        </div>
    
        
        <section id="content">
            <h1 id="c代码">C代码</h1>
<h2 id="创建定时任务">创建定时任务</h2>
<p>发现代码，win有自动防范木马的可能，于是要手动去在定时任务勾选某些指标，所以不可行。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e"># include&lt;stdio.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"># include &lt;windows.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>  <span style="color:#75715e">//如果存在任务名为myTaskName2 则强制删除，若没有则报错，但不终止该程序的运行
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName2 /f&#34;</span>); 
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">// 创建一个任务名为myTaskName2的任务,/tr 后面是 要执行的命令， 双引号要转义，/st 后面接的是触发时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName2  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 22:35:00&#34;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName4 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName4  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 22:42:00&#34;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName3 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName3  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 22:52:00&#34;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName5 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName6 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName7 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName5  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 23:00:00&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName6  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 23:15:00&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName7  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 23:27:00&#34;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName8 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /delete /tn myTaskName9 /f&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName8  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 23:43:00&#34;</span>);
</span></span><span style="display:flex;"><span>	system(<span style="color:#e6db74">&#34;schtasks /create /tn myTaskName9  /f /tr </span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74">shutdown -s -f -t 10</span><span style="color:#ae81ff">\&#34;</span><span style="color:#e6db74"> /sc once /st 23:58:00&#34;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h1 id="编译运行后">编译运行后</h1>
<p>编译运行后，找到对应得exe文件 ,把其放在开始启动目录中即可（<code>C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp</code>）</p>
<p>或者 写一个bat命令，放在开始启动目录，eg：
<code>start &quot;&quot; &quot;E:\TIM\xin\Bin\TIM.exe&quot; &amp; ping localhost -n 5</code></p>
<p>命令中的<code>&amp;</code> <code>&amp;&amp;</code> <code>||</code> 分别的含义：<br>
其中：</p>
<ul>
<li>&amp; : 用来分隔一个命令行中的多个命令,即运行第一个命令，然后运行第二个命令。</li>
<li>&amp;&amp; ： 只有在符号 &amp;&amp; 前面的命令成功时，才用于运行该符号后面的命令</li>
<li>|| : 只有在符号 || 前面的命令失败时，才用于运行符号 || 后面的命令。</li>
</ul>
<h2 id="直接注入关机命令">直接注入关机命令</h2>
<p>这样感觉，电脑右下角一直有一个图标，
于是再次改进</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e"># include&lt;stdio.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;time.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span><span style="color:#75715e">&lt;stdlib.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;string.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span><span style="color:#75715e">&lt;windows.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">loop_shut</span>(){
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">while</span>(<span style="color:#ae81ff">1</span>){
</span></span><span style="display:flex;"><span>		system(<span style="color:#e6db74">&#34;shutdown -s -f -t 5&#34;</span>);
</span></span><span style="display:flex;"><span>		Sleep(<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">char</span> str1[<span style="color:#ae81ff">100</span>]<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;shutdown -s -f -t &#34;</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">char</span> str2[<span style="color:#ae81ff">32</span>];
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">char</span> <span style="color:#f92672">*</span>str3; 
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">int</span> now_hour<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,now_min<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,now_second<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">struct</span> tm <span style="color:#f92672">*</span> tmpointer;  <span style="color:#75715e">//申明tm结构体指针
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	time_t secs;  <span style="color:#75715e">//申明time_t类型变量
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	time(<span style="color:#f92672">&amp;</span>secs); <span style="color:#75715e">//获取当前系统日期与时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	tmpointer <span style="color:#f92672">=</span> localtime(<span style="color:#f92672">&amp;</span>secs); <span style="color:#75715e">//获取tm结构的日期时间,获取当地日期与时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	printf(<span style="color:#e6db74">&#34;\Now: %d-%d-%d %d:%d:%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,tmpointer<span style="color:#f92672">-&gt;</span>tm_mon,tmpointer<span style="color:#f92672">-&gt;</span>tm_mday,
</span></span><span style="display:flex;"><span>	tmpointer<span style="color:#f92672">-&gt;</span>tm_year<span style="color:#f92672">+</span><span style="color:#ae81ff">1900</span>,tmpointer<span style="color:#f92672">-&gt;</span>tm_hour,tmpointer<span style="color:#f92672">-&gt;</span>tm_min,tmpointer<span style="color:#f92672">-&gt;</span>tm_sec); 
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//获取当前系统时间 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	now_hour <span style="color:#f92672">=</span> tmpointer<span style="color:#f92672">-&gt;</span>tm_hour;
</span></span><span style="display:flex;"><span>	now_min <span style="color:#f92672">=</span> tmpointer<span style="color:#f92672">-&gt;</span>tm_min;
</span></span><span style="display:flex;"><span>	now_second <span style="color:#f92672">=</span> tmpointer<span style="color:#f92672">-&gt;</span>tm_sec;
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">当前时间:%d:%d:%d&#34;</span>,tmpointer<span style="color:#f92672">-&gt;</span>tm_hour,tmpointer<span style="color:#f92672">-&gt;</span>tm_min,tmpointer<span style="color:#f92672">-&gt;</span>tm_sec);
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//设置关机时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">int</span> close_hour <span style="color:#f92672">=</span> <span style="color:#ae81ff">22</span>,close_min <span style="color:#f92672">=</span><span style="color:#ae81ff">30</span>,close_sec <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>	printf(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">关机时间:%d:%d:%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,close_hour,close_min,close_sec);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">int</span> i<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,j<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">int</span> diff_hour ,diff_min;<span style="color:#75715e">//时间差
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">int</span> left_time ;<span style="color:#75715e">//离关机时间还剩多少秒 (单位:秒)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	diff_hour <span style="color:#f92672">=</span> now_hour <span style="color:#f92672">-</span> close_hour; <span style="color:#75715e">//离关机时间还剩多少个小时 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	diff_min <span style="color:#f92672">=</span> now_min <span style="color:#f92672">-</span>close_min;<span style="color:#75715e">// 离关机时间还剩多少个分钟 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	printf(<span style="color:#e6db74">&#34;时间差: 小时:%d  分钟:%d </span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,diff_hour,diff_min);
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//时间在 关机时间后 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span>(diff_hour <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">0</span>) {
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">// 当前小时在关机时间之后  
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		printf(<span style="color:#e6db74">&#34;正在关机1</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>		system(<span style="color:#e6db74">&#34;shutdown -s -f -t 10&#34;</span>);<span style="color:#75715e">//立即关机 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		loop_shut(); 
</span></span><span style="display:flex;"><span>			
</span></span><span style="display:flex;"><span>	}<span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span>(diff_hour <span style="color:#f92672">==</span><span style="color:#ae81ff">0</span>){
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">//当前小时 正在关机时间处 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		<span style="color:#66d9ef">if</span>(diff_min <span style="color:#f92672">&lt;</span><span style="color:#ae81ff">0</span>){
</span></span><span style="display:flex;"><span>			<span style="color:#75715e">//没超过关机时间 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			printf(<span style="color:#e6db74">&#34;正在关机2</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>			diff_min <span style="color:#f92672">=</span>(<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>) <span style="color:#f92672">*</span> diff_min;
</span></span><span style="display:flex;"><span>			i <span style="color:#f92672">=</span> diff_min<span style="color:#f92672">*</span><span style="color:#ae81ff">60</span>;
</span></span><span style="display:flex;"><span>			printf(<span style="color:#e6db74">&#34;还剩%d秒==%d分钟</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,i,diff_min);
</span></span><span style="display:flex;"><span>			itoa(i,str2,<span style="color:#ae81ff">10</span>);<span style="color:#75715e">//把数字转变为字符串 ，后面的10 为10进制 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			strcat(str1,str2);  <span style="color:#75715e">//拼接字符串 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			system(str1);
</span></span><span style="display:flex;"><span>		}<span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span>(diff_min <span style="color:#f92672">==</span><span style="color:#ae81ff">0</span>){
</span></span><span style="display:flex;"><span>			printf(<span style="color:#e6db74">&#34;正在关机3</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>			system(<span style="color:#e6db74">&#34;shutdown -s -f -t 20&#34;</span>);<span style="color:#75715e">//立即关机 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			loop_shut();
</span></span><span style="display:flex;"><span>		}<span style="color:#66d9ef">else</span>{
</span></span><span style="display:flex;"><span>			<span style="color:#75715e">//超过关机时间 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			printf(<span style="color:#e6db74">&#34;正在关机4</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>			system(<span style="color:#e6db74">&#34;shutdown -s -f -t 20&#34;</span>);<span style="color:#75715e">//立即关机 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			loop_shut();
</span></span><span style="display:flex;"><span>		}
</span></span><span style="display:flex;"><span>	}<span style="color:#66d9ef">else</span>{
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">//当前小时 在关机时间之前 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		printf(<span style="color:#e6db74">&#34;正在关机5</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>		left_time <span style="color:#f92672">=</span> diff_hour<span style="color:#f92672">*</span>(<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>)<span style="color:#f92672">*</span><span style="color:#ae81ff">60</span> <span style="color:#f92672">+</span> diff_min<span style="color:#f92672">*</span>(<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>		i <span style="color:#f92672">=</span> left_time<span style="color:#f92672">*</span><span style="color:#ae81ff">60</span>;
</span></span><span style="display:flex;"><span>		printf(<span style="color:#e6db74">&#34;还剩%d秒==%d分钟</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,i,left_time);
</span></span><span style="display:flex;"><span>		itoa(i,str2,<span style="color:#ae81ff">10</span>);<span style="color:#75715e">//把数字转变为字符串 ，后面的10 为10进制 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		strcat(str1,str2);  <span style="color:#75715e">//拼接字符串 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		system(str1);
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>; 
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="结合定时任务关机">结合定时任务关机</h2>
<p>到某个时间以后，如果在重启，就会直接关机（除非你手速快）,并把生成的exe文件设置为开机启动。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e"># include &lt;stdio.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"># include &lt;time.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"># include &lt;stdlib.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"># include &lt;string.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"># include &lt;windows.h&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">loop_shut</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">while</span>(<span style="color:#ae81ff">1</span>) {
</span></span><span style="display:flex;"><span>		system(<span style="color:#e6db74">&#34;shutdown -s -f -t 5&#34;</span>);
</span></span><span style="display:flex;"><span>		Sleep(<span style="color:#ae81ff">6</span>);
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//设置关机时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">int</span> close_hour <span style="color:#f92672">=</span> <span style="color:#ae81ff">22</span>,close_min <span style="color:#f92672">=</span><span style="color:#ae81ff">35</span>,close_sec <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">int</span> diff_hour ,diff_min;<span style="color:#75715e">//声明变量用来存储时间差
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">int</span> temp;
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//声明变量获取时间结构
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">int</span> now_hour<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,now_min<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,now_second<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">struct</span> tm <span style="color:#f92672">*</span> tmpointer;  <span style="color:#75715e">//申明tm结构体指针
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	time_t secs;  <span style="color:#75715e">//申明time_t类型变量
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	time(<span style="color:#f92672">&amp;</span>secs); <span style="color:#75715e">//获取当前系统日期与时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	tmpointer <span style="color:#f92672">=</span> localtime(<span style="color:#f92672">&amp;</span>secs); <span style="color:#75715e">//获取tm结构的日期时间,获取当地日期与时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//获取当前系统时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	now_hour <span style="color:#f92672">=</span> tmpointer<span style="color:#f92672">-&gt;</span>tm_hour;
</span></span><span style="display:flex;"><span>	now_min <span style="color:#f92672">=</span> tmpointer<span style="color:#f92672">-&gt;</span>tm_min;
</span></span><span style="display:flex;"><span>	now_second <span style="color:#f92672">=</span> tmpointer<span style="color:#f92672">-&gt;</span>tm_sec;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//时间差
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	diff_hour <span style="color:#f92672">=</span> now_hour <span style="color:#f92672">-</span> close_hour; <span style="color:#75715e">//离关机时间还剩多少个小时
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	diff_min <span style="color:#f92672">=</span> now_min <span style="color:#f92672">-</span>close_min;<span style="color:#75715e">// 离关机时间还剩多少个分钟
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#75715e">//关机程序，时间在22:30分以后 以及早上7点之前
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	temp <span style="color:#f92672">=</span> ( diff_hour <span style="color:#f92672">&gt;</span><span style="color:#ae81ff">0</span> <span style="color:#f92672">||</span>(diff_hour <span style="color:#f92672">==</span><span style="color:#ae81ff">0</span> <span style="color:#f92672">&amp;&amp;</span> diff_min<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">0</span>) ) <span style="color:#f92672">||</span> (now_hour <span style="color:#f92672">&lt;</span><span style="color:#ae81ff">7</span>); 
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span>(temp) {
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">// 已过关机时间之后
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		printf(<span style="color:#e6db74">&#34;正在关机1</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">//system(&#34;shutdown -s -f -t 5&#34;);//立即关机 ,有时候需要保存一点重要资料
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		loop_shut();
</span></span><span style="display:flex;"><span>	} <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>		printf(<span style="color:#e6db74">&#34;不关机!&#34;</span>);
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>同时在win的程序计划任务中自己添加几个任务，到点自动关机，注意第一个留一点时间，保存文件，后面一个直接关机.并设置选项，不然不会自动运行
如下:</p>
<p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/guanji01.png" alt=""></p>
<p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/img/guanji02.png" alt=""></p>
<p>亲试，根本还没有看到桌面，就显示关机了.</p>

        </section>
    </div>
    <br>
    
    




<span id="/md/2018-11-20-c%E8%AF%AD%E8%A8%80/" class="leancloud_visitors" data-flag-title="用c语言设置定时关机">
  <span class="post-meta-item-text">文章总阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次;
  <p></p>
</span>



    

    
    
    <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit" aria-haspopup="true" aria-expanded="false" aria-controls="edit">
        <i class="fa fa-edit">编辑本文</i>
    </button>
    
    
    

    <br>
    <hr>
    <li style="float:left;list-style:none">
        
        <a class="previous" href="/md/2018-11-19-%E8%BF%90%E7%AD%B9%E5%AD%A6%E4%B8%8E%E6%9C%80%E4%BC%98%E5%8C%96matlab/"> 上一篇: 运筹学与最优化matlab</a>
        
    </li>
    <li style="float:right;list-style:none">
        
        <a class="next" href="/md/2018-11-22-%E8%BF%90%E7%AD%B9%E5%AD%A6%E6%9C%80%E7%9F%AD%E8%B7%AF%E9%97%AE%E9%A2%98/"> 下一篇: 运筹学最短路问题</a>
        
    </li>
     
    
    <script src="/js/copyCode.js"></script>
    <script src="/js/tooltips.js"></script>
    
   
    <script>
    [].slice.call(document.querySelectorAll('table')).forEach(function(el) {
        var wrapper = document.createElement('div');
        wrapper.className = 'table-area';
        el.parentNode.insertBefore(wrapper, el);
        el.parentNode.removeChild(el);
        wrapper.appendChild(el);
        $("table").wrap("<div class='table-area'></div>");
    })
    </script>

    
<br>
<hr>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-111691389-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-111691389-1');
</script>




      
      
      

       
      
      
      <script>
              document.getElementById("edit-button").addEventListener("click", function () {
                  var editWindow = window.open("https:\/\/github.com\/zoushucai\/blogmmm/edit/master/content/md\/2018-11-20-C语言.md");
              });</script>
      
          




<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  } 
</script>



    </style>
    <script type="text/javascript">
    function showdiv(){
        document.getElementById("divtocTableOfContents").style.display="block";
        document.getElementById("strHref").innerHTML="目录收起-";
        document.getElementById('divTableOfContents').style.width="22%";
        document.getElementById('divTableOfContents').style.height="55%";
        document.getElementById('divTableOfContents').style.top="25%";
        document.getElementById('divTableOfContents').style.bottom="5%";
        document.getElementById("strHref").href="javascript:hidediv()";
    }
    function hidediv(){
        document.getElementById("divtocTableOfContents").style.display="none";
        document.getElementById("strHref").innerHTML="目录展开+";
        document.getElementById("strHref").href="javascript:showdiv()";
        document.getElementById('divTableOfContents').style.width="10%";
        document.getElementById('divTableOfContents').style.height="5%";
    }
    </script>
</body>

</html>
</div> 







    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-LJ2FmexL77rmGm6SIpxq7y+XA6bkLzGZEgCywzKOZG/ws4va9fUVu2neMjvc3zdv" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            renderMathInElement(document.body, {
            delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "$", right: "$", display: false},
                            {left: "\\(", right: "\\)", display: false},
                            {left: "\\[", right: "\\]", display: true}
                        ]
            });
        });
    </script>













<br>
<div class="inner">
              
            
          
          
  
          
  
  <div id="vcomments"></div>
  
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'HfHPKPkLa0cBEDPcdBAHuqMv-gzGzoHsz',
        appKey: 'r5RJAasN8e0mB9sq6y9pEcX0',
        lang:'zh-CN',
        notify:  false , 
        verify:  false  ,
        avatar:'identicon', 
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

</div>

<br>
<br>
<footer>
    <p style="float:right;margin-right: 5%;margin-top: 0%;">
        &copy; 2022 <a href="https://github.com/zoushucai">zsc</a>
      </p>
</footer>
<br>
<br>


